package com.relytosoft.mizar.analyze.face.services.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.mizar.analyze.face.api.entity.AyFaceDisposition;
import com.relytosoft.mizar.analyze.face.api.pojo.dto.AyFaceDispositionDto;
import com.relytosoft.mizar.analyze.face.api.pojo.query.AyFaceSearchQuery;
import com.relytosoft.mizar.analyze.face.api.pojo.vo.AyFaceDispositionVo;
import com.relytosoft.mizar.analyze.face.services.service.AyFaceDispositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.simpleframework.xml.core.Validate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @date 2022/5/20 10:28
 */
@Api(tags = {"人脸布控任务"})
@RestController
@RequestMapping("/face/disposition")
public class AyFaceDispositionController {

    @Resource
    private AyFaceDispositionService ayFaceDispositionService;

    @ApiOperation("查询")
    @GetMapping("list")
    public ResponseResult<Map<String, Object>> list(@RequestParam("condition") String condition,
                                                    @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyFaceDispositionDto ayFaceDispositionDto = JSONObject.parseObject(condition, AyFaceDispositionDto.class);
        AyFaceDisposition ayFaceDisposition = BeanCopyUtils.copy(ayFaceDispositionDto, AyFaceDisposition.class);
        IPage<AyFaceDisposition> page = ayFaceDispositionService.selectAyFaceDispositionPageList(pageQuery, ayFaceDisposition);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @ApiOperation("添加")
    @PostMapping("add")
    public ResponseResult<Void> add(@Validate @RequestBody AyFaceDispositionDto ayFaceDispositionDto) {
        //获取需要添加布控任务的数据
        List<AyFaceDisposition> list = ayFaceDispositionService.getAyFaceDispositionInfo(ayFaceDispositionDto);
        //添加布控任务到数据库
        List<AyFaceDisposition> addSuccess = ayFaceDispositionService.addAyFaceDisposition(list);
        if (addSuccess.isEmpty()) {
            return ResponseResult.fail("添加布控任务失败");
        }
        //发送消息到kafka添加布控任务
        ayFaceDispositionService.sendAddInfoToKafka(addSuccess);
        return ResponseResult.ok();
    }

    @ApiOperation("修改")
    @PutMapping("update")
    public ResponseResult<Void> update(@Validate @RequestBody AyFaceDispositionDto ayFaceDispositionDto) {
        boolean isUpdateSuccess = ayFaceDispositionService.updateAyFaceDisposition(ayFaceDispositionDto);
        if (isUpdateSuccess) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.fail("修改布控任务失败");
        }
    }

    @ApiOperation("删除")
    @DeleteMapping("remove")
    public ResponseResult<Map<Integer, String>> remove(@RequestBody List<Integer> list) {
        Map<Integer, String> map = ayFaceDispositionService.removeAyFaceDisposition(list);
        return ResponseResult.ok(map);
    }

    @ApiOperation("详情")
    @GetMapping("{id}")
    public ResponseResult<AyFaceDispositionVo> getDetails(@PathVariable Integer id) {
        return ResponseResult.ok(ayFaceDispositionService.selectAyFaceDispositionById(id));
    }

    @ApiOperation("停止布控任务")
    @GetMapping("stop/{id}")
    public ResponseResult<Void> stopDispositionTask(@PathVariable Integer id) {
        boolean isStopTask = ayFaceDispositionService.stopTask(id);
        if (isStopTask) {
            return ResponseResult.ok("停止任务成功");
        } else {
            return ResponseResult.fail("当前任务未启动");
        }
    }

    @ApiOperation("启用布控任务")
    @GetMapping("start/{id}")
    public ResponseResult<Void> startDispositionTask(@PathVariable Integer id) {
        return ResponseResult.ok(ayFaceDispositionService.startTask(id));
    }


    @ApiOperation("布防撤防")
    @PostMapping("armed/disarmed")
    public ResponseResult<Void> armedAndDisarmed(@RequestBody List<Integer> list) {
        boolean isSuccess = ayFaceDispositionService.armedAndDisarmed(list);
        if (isSuccess) {
            return ResponseResult.ok("布/撤防成功");
        } else {
            return ResponseResult.fail("布/撤防失败");
        }
    }

    @ApiOperation("人脸检索")
    @GetMapping("face/search")
    public ResponseResult<JSONObject> faceSearch(@RequestParam("condition") String condition, @RequestParam("pageQuery") String query) {
        PageQuery pageQuery = JSON.parseObject(query, new TypeReference<PageQuery>() {
        });
        AyFaceSearchQuery ayFaceSearchQuery = JSONObject.parseObject(condition, AyFaceSearchQuery.class);
        return ResponseResult.ok(ayFaceDispositionService.faceSearch(ayFaceSearchQuery, pageQuery));
    }


    @ApiOperation("临时人脸库布防")
    @PostMapping("temp")
    public ResponseResult<Void> addTempFace(@RequestBody AyFaceDispositionDto ayFaceDispositionDto) {
        boolean face = ayFaceDispositionService.addTempFace(ayFaceDispositionDto);
        if (face) {
            return ResponseResult.ok("临时人脸库布防成功");
        } else {
            return ResponseResult.fail("临时人脸库布防失败");
        }
    }
}
